package sun.springboot.quartz_scheduler_integrate.web;

import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.springboot.quartz_scheduler_integrate.core.CronSchedulerServer;
import sun.springboot.quartz_scheduler_integrate.core.job.TipsJob;
import sun.springboot.quartz_scheduler_integrate.core.model.JobConf;
import sun.springboot.quartz_scheduler_integrate.core.model.TriggerConf;

import java.util.HashMap;
import java.util.Map;

/**
 * 调度测试
 */
@RestController
public class QuartSchedulerController {

    private CronSchedulerServer cronSchedulerServer;

    public QuartSchedulerController(CronSchedulerServer cronSchedulerServer) {
        this.cronSchedulerServer = cronSchedulerServer;
    }

    private final static String triggerGroup = "triggerGroup1";
    private final static String triggerName = "triggerName1";
    private final static String cron = "*/2 * * * * ?";
    private final static String jobGroup = "jobGroup1";
    private final static String jobName = "jobName1";

    /**
     * 添加调度任务
     */
    @GetMapping("/scheduler/add")
    public void addScheduler() throws SchedulerException {
        /*job传送map数据*/
        Map<String, Object> map = new HashMap<>();
        map.put("title", "hello word");
        cronSchedulerServer.addScheduler(new TriggerConf(triggerName, triggerGroup, cron), new JobConf(jobGroup, jobName, new TipsJob(), map));
    }

    /**
     * 暂停触发器
     */
    @GetMapping("/scheduler/pause")
    public void pauseTrigger() throws SchedulerException {
        cronSchedulerServer.pauseTrigger(new TriggerConf(triggerName, triggerGroup));
    }

    /**
     * 继续触发器
     */
    @GetMapping("/scheduler/resume")
    public void resumeTrigger() throws SchedulerException {
        cronSchedulerServer.resumeTrigger(new TriggerConf(triggerName, triggerGroup));
    }

    /**
     * 删除触发器
     */
    @GetMapping("/scheduler/del")
    public void unScheduleJob() throws SchedulerException {
        cronSchedulerServer.unScheduleJob(new TriggerConf(triggerName, triggerGroup));
    }
}
